package org.vincen.helloworld.jdbc;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Properties;

public class ReadProperties {
	
	private Properties properties;
	private String path = this.getClass().getResource("").getPath();
	private String fileName = "database.properties";
	
	//解析配置文件，获得url，username，password
	public String[] getProperties () throws IOException {
		
		String[] databaseInfo = new String[3];
		properties = new Properties();
		String folderDirectory = URLDecoder.decode(path, "gbk");
		String fileDirectory = folderDirectory + fileName;
		FileInputStream in = null;
		try {
			in = new FileInputStream(fileDirectory);
			properties.load(in);
		} finally {
			in.close();
		}

		//使用jdbc4.0的新特性，不需要显示的加载jdbc。drivers，service provider机制自动加载数据库驱动类
//		String driver = properties.getProperty("jdbc.drivers");
//		if (driver != null) {
//			System.setProperty("jdbc.drivers", driver);
//		}
		databaseInfo[0] = properties.getProperty("jdbc.url");
		databaseInfo[1] = properties.getProperty("jdbc.username");
		databaseInfo[2] = properties.getProperty("jdbc.password");
		
		return databaseInfo;
	}
}
